package com.rentong.cust.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.rentong.common.annotation.Anonymous;
import com.rentong.common.core.page.PageDomain;
import com.rentong.common.core.page.TableSupport;
import com.rentong.cust.domain.CustJiesuanVo;
import com.rentong.cust.utils.PageByList;
import com.rentong.cust.vo.CustJiesuanRequest;
import com.rentong.cust.vo.SubmitRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rentong.common.annotation.Log;
import com.rentong.common.core.controller.BaseController;
import com.rentong.common.core.domain.AjaxResult;
import com.rentong.common.enums.BusinessType;
import com.rentong.cust.domain.CustJiesuanList;
import com.rentong.cust.service.ICustJiesuanListService;
import com.rentong.common.utils.poi.ExcelUtil;
import com.rentong.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 结算单Controller
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@Anonymous
@RestController
@RequestMapping("/custJiesuanList/custJiesuanList")
public class CustJiesuanListController extends BaseController {
    @Autowired
    private ICustJiesuanListService custJiesuanListService;

    /**
     * 查询结算单列表
     */
//    //('custJiesuanList:custJiesuanList:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustJiesuanRequest custJiesuanRequest) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<CustJiesuanVo> list = custJiesuanListService.selectCustJiesuanListList(custJiesuanRequest);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(PageByList.startPage(list, pageNum, pageSize));
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }


    /**
     * 查询确认单列表
     */
//    //('custJiesuanList:custJiesuanList:list')")
    @GetMapping("/confirmList")
    public TableDataInfo confirmList(CustJiesuanRequest custJiesuanRequest) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<CustJiesuanVo> list = custJiesuanListService.selectCustJiesuanListListByConfirm(custJiesuanRequest);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(PageByList.startPage(list, pageNum, pageSize));
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

//    /**
//     * 导出结算单列表
//     */
//    //('custJiesuanList:custJiesuanList:export')")
//    @Log(title = "结算单", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, CustJiesuanList custJiesuanList) {
//        List<CustJiesuanList> list = custJiesuanListService.selectCustJiesuanListList(custJiesuanList);
//        ExcelUtil<CustJiesuanList> util = new ExcelUtil<CustJiesuanList>(CustJiesuanList.class);
//        util.exportExcel(response, list, "结算单数据");
//    }

    /**
     * 获取结算单详细信息
     */
//    //('custJiesuanList:custJiesuanList:query')")
    @GetMapping(value = "/{jiesuanListId}")
    public AjaxResult getInfo(@PathVariable("jiesuanListId") Long jiesuanListId) {
        return success(custJiesuanListService.selectCustJiesuanListByJiesuanListId(jiesuanListId));
    }

    /**
     * 新增结算单
     */
//    //('custJiesuanList:custJiesuanList:add')")
    @Log(title = "结算单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustJiesuanList custJiesuanList) {
        return toAjax(custJiesuanListService.insertCustJiesuanList(custJiesuanList));
    }

    /**
     * 修改结算单
     */
//    //('custJiesuanList:custJiesuanList:edit')")
    @Log(title = "结算单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustJiesuanList custJiesuanList) {
        return toAjax(custJiesuanListService.updateCustJiesuanList(custJiesuanList));
    }

    /**
     * 删除结算单
     */
//    //('custJiesuanList:custJiesuanList:remove')")
    @Log(title = "结算单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jiesuanListIds}")
    public AjaxResult remove(@PathVariable Long[] jiesuanListIds) {
        return toAjax(custJiesuanListService.deleteCustJiesuanListByJiesuanListIds(jiesuanListIds));
    }

    //excel结算单导入
    @PostMapping("/upload1")
    public AjaxResult handleFileUpload(@RequestParam("file") MultipartFile file) throws IOException {
        custJiesuanListService.handleFileUpload(file);
        return AjaxResult.success();
    }

    //提交结算单
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody SubmitRequest submitRequest) {
        custJiesuanListService.submit(submitRequest);
        return null;
    }

    //结算单确认
    @PostMapping("/confirmSubmit")
    public AjaxResult confirmSubmit(@RequestBody SubmitRequest submitRequest) {
        custJiesuanListService.confirmSubmit(submitRequest);
        return null;
    }

    //退回结算单
    @DeleteMapping("/out/{jiesuanListIds}")
    public AjaxResult outConfirm(@PathVariable Long[] jiesuanListIds) {
        custJiesuanListService.outConfirm(jiesuanListIds);
        return null;
    }

}
